package com.api.gbhg.lbj.action;

import com.api.gbhg.lbj.dao.LoanrepaymentDao;
import com.api.gbhg.lbj.dao.ReimbursementDao;
import com.api.gbhg.zengping.common.CommonDao;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import weaver.conn.RecordSet;
import weaver.conn.RecordSetTrans;
import weaver.general.BaseBean;
import weaver.interfaces.workflow.action.Action;
import weaver.soa.workflow.request.RequestInfo;
import weaver.workflow.workflow.WorkflowVersion;

import java.util.HashMap;
import java.util.Map;

import static com.api.gbhg.liwenjun.VersionFlowUtil.getFlowTableName;

/**
 * @author lbj
 * @version 1.0
 * @description: TODO 还款流程
 * @date 2021/5/28 16:45
 */
public class RepaymentAction implements Action {
    private static final Logger logger = LoggerFactory.getLogger("gbhg");

    @Override
    public String execute(RequestInfo requestInfo) {
        logger.info("进入-还款流程-RepaymentAction---");
        //请求id
        String requestid = requestInfo.getRequestid();
        //获取表名
        String billTableName = requestInfo.getRequestManager().getBillTableName();

        LoanrepaymentDao loa = new LoanrepaymentDao();
        //还款流程主表数据
        RecordSet rs = CommonDao.selectByRequestId(new RecordSet(), billTableName, requestid);
        RecordSetTrans rst = new RecordSetTrans();
        rst.setAutoCommit(false);
        if (rs.next()) {
            String gljklc = rs.getString("gljklc");//关联借款流程
            String hkje = rs.getString("hkje");//还款金额
            logger.info("关联借款流程：" + gljklc + ",还款金额:" + hkje);

            BaseBean bean = new BaseBean();
            //获取借款流程Workflowid
            String YS27Workflowid = WorkflowVersion.getActiveVersionWFID(bean.getPropValue("ReimbursementWorkflowid", "YS27Workflowid"));//读取配置文件
            logger.info("获取借款流程Workflowid：" + YS27Workflowid);

            String flowTableName = getFlowTableName(YS27Workflowid);
            logger.info("根据Workflowid获取借款流程表名:" + flowTableName);

            //查询借款流程主表数据
            RecordSet rsjk = CommonDao.selectByRequestId(new RecordSet(), flowTableName, gljklc);

            if (rsjk.next()) {
                Map<String, String> list = new HashMap<>();
                String ysxm = rsjk.getString("ysxm");
                String bmjjzcfl = rsjk.getString("bmjjzcfl");
                String zcqd = rsjk.getString("zcqd");
                String ysly = rsjk.getString("ysly");
                String fzysrfl = rsjk.getString("fzysrfl");
                String xzkmdm = rsjk.getString("xzkmdm");
                String zcdfczbkxm = rsjk.getString("zcdfczbkxm");
                String nd = rsjk.getString("nd");
                String dw = rsjk.getString("szdlhsdw");
                String jbbm = rsjk.getString("jbbm");

                list.put("zcqd", zcqd);//支出渠道
                list.put("ysly", ysly);//预算来源
                list.put("nd", nd);//年度
                list.put("fzysrfl", fzysrfl);//非中央收入分类
                list.put("xzkmdm", xzkmdm);//选择科目代码
                list.put("zcdfczbkxm", zcdfczbkxm);//地方财政拨款项目
                list.put("dw", dw);//单位
                list.put("jbbm", jbbm);//经办部门
                list.put("lcjlid", ysxm);//预算项目
                list.put("money", hkje);//还款金额
                list.put("bmzcjjfl", bmjjzcfl);//部门经济支出分类
                logger.info("预算项目:" + ysxm + "还款金额:" + hkje + "部门经济支出分类:" + bmjjzcfl);
                boolean b = true;
                try {
                    b = loa.jhkMainJingfei(rst,list, false); // jingfei表 true:扣款  false:还款
                    if (b) {
                        b = loa.jhkjingfeily(rst,list, false);// jinfei来源和收入 true:扣款  false:还款
                    }
                    logger.info("b:" + b);
                } catch (Exception e) {
                    e.printStackTrace();
                }
                if (!b) {
                    rst.rollback();
                    logger.info("=====================还款失败=======================");
                    requestInfo.getRequestManager().setMessagecontent("金额还款失败");
                    requestInfo.getRequestManager().setMessageid("1");
                    return FAILURE_AND_CONTINUE;
                }
            }
        }
        rst.commit();
        return SUCCESS;
    }
}
